var http = require('http');
var querystring = require('querystring');

var allmess={
    CompanyId:'0010',
    Source:'wyc1',
    IPCType:'baseInfoCompany',
    baseInfoCompany:
        [

            {"id":5,"CompanyId":"0313","CompanyName":"淄博饭店集团淄博群方出租车有限公司", "Identifier":"1236548214","Address": 2222222,"ContactAddress":"张店区中心路177号","EconomicType":"141","RegCapital":"100","LegalName":"法人1","LegalID":"372501","LegalPhone":"0533-2180888","LegalPhoto":"","State":0,"Flag":0,"UpdateTime":"20180502092830" },
            {"id":4,"CompanyId":"0702","CompanyName":"桓台爱使汽车服务有限公司", "Identifier":"123456","Address":370301,"ContactAddress":"contactaddress4","EconomicType":"141","RegCapital":"104","LegalName":"法人2","LegalID":"372504","LegalPhone":"0533-2312627","LegalPhoto":"","State":0,"Flag":0,"UpdateTime":"20180502092830" },
            {"id":3,"CompanyId":"0801","CompanyName":"高青县恒通汽车运输有限责任公司", "Identifier":"12345","Address":370301,"ContactAddress":"contactaddress2","EconomicType":"141","RegCapital":"103","LegalName":"法人3","LegalID":"372503","LegalPhone":"0533-2312628","LegalPhoto":"","State":0,"Flag":0,"UpdateTime":"20180502092830" },
            {"id":2,"CompanyId":"0901","CompanyName":"沂源大众交通运输有限责任公司", "Identifier":"131306321244","Address":370301,"ContactAddress":"沂源县交通局南邻","EconomicType":"141","RegCapital":"100","LegalName":"法人4","LegalID":"372502","LegalPhone":"0533-3433456","LegalPhoto":"","State":0,"Flag":0,"UpdateTime":"20180502092830" }
        ]

};


var http = require('http');
var ProtoBuf = require("protobufjs");

// 同步创建proto，异步用ProtoBuf.load("./proto/a.proto",function(err,root){...})
var root = ProtoBuf.loadSync("./wycproto/company.proto");
// 获得一个包类型
var textMessage = root.lookupType("wyc.CompanyMess");



// 检测是否符合类型
var errMsg = textMessage.verify(allmess);
if (errMsg) throw Error(errMsg);

// 生成一条message
var message = textMessage.create(allmess); // or use .fromObject if conversion is necessary

// 将message转成buffer,node下转成Buffer,browser下转成Uint8Array
var buffer = textMessage.encode(message).finish();
console.log(buffer.length);

var temp=textMessage.decode(buffer);
console.log(temp);



var options = {
    host:'localhost',
    path:'/baseinfo/company',  port:8081,
    method:'POST',
    headers:{
        'Content-Type':'application/x-protobuf',
        'Content-Length':buffer.length
    }
}

var req = http.request(options, function(res){
    res.setEncoding('utf8');
    res.on('data',function(data){
        console.log("data:",data);   //一段html代码
    });
});

req.on("timeout", function() {
    console.log("timeout received");
    if (req.res) {
        req.res.emit("abort");
    }

    req.abort();
});


req.write(buffer);
req.end;
